package com.sdry.service.impl.cb;

import com.sdry.mapper.cb.HydroPowerMapper;
import com.sdry.model.cb.HydroPower;
import com.sdry.service.cb.HydroPowerService;
import com.sdry.utils.BigDecimalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class HydroPowerServiceImpl implements HydroPowerService {

    @Autowired
    private HydroPowerMapper hydroPowerMapper;

    @Override
    public List<HydroPower> getList(Long stockId, String date) {
        return hydroPowerMapper.findPageWithResult(stockId,date);
    }

    @Override
    public Integer add(HydroPower hydroPower){
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        //价格
        hydroPower.setPrice(new BigDecimal(hydroPower.getParameter2()));
        //时间
        try {
            hydroPower.setCreatedate(new Timestamp(sdf.parse(hydroPower.getParameter3()+" 00:00:00.000").getTime()));
        }catch (Exception e){
            hydroPower.setCreatedate(new Timestamp(new Date().getTime()));
        }

        //房租
        String multiply = BigDecimalUtil.multiply(hydroPower.getWarehousequantity().toString(), hydroPower.getPrice().toString());
        //水费 3%
        hydroPower.setWater(new BigDecimal(BigDecimalUtil.multiply(multiply,"0.03")));
        //电费 13%
        hydroPower.setPower(new BigDecimal(BigDecimalUtil.multiply(multiply,"0.13")));
        return hydroPowerMapper.insertDynamic(hydroPower);
    }

    @Override
    public Integer delete(String[] idArr) {
        Integer affectRow = 0;
        for (int i = 0; i < idArr.length; i++) {
            affectRow = hydroPowerMapper.setDelete(Long.parseLong(idArr[i]));
        }
        return affectRow;
    }
}
